package com.speedchina.common.download.utils;

import com.alibaba.excel.metadata.CellData;
import com.speedchina.common.download.base.FieldConversion;
import com.speedchina.common.download.emnu.ConversionTypeEnum;
import com.speedchina.framework.utils.ReflectionUtils;

import java.util.List;

/**
 * @Author xz
 * Data: 2021/5/26
 */
public class ListConvertUtils {
    /**
     * @author: xz
     * @date:  2021/5/25
     * [fieldConversionList, data] fieldConversionList 转换信息；data 数据
     * @return void
     * @throws
     */
    public static void buildData(List<FieldConversion> fieldConversionList, List data){
        for (Object datum : data) {
            for (FieldConversion fieldConversion : fieldConversionList) {
                try {
                    //根据转换器重新设置值，数据转换，目前只支持String的返回
                    CellData cellData = fieldConversion.getConverter().convertToExcelData(ReflectionUtils.getFieldValue(datum, fieldConversion.getFieldName()), null, null);
                    if (ConversionTypeEnum.CONVERT_TO_STRING.getCode().equals(fieldConversion.getType())){
                        ReflectionUtils.setFieldValue(datum,fieldConversion.getFieldName(),cellData.getStringValue());
                    }
                    if (ConversionTypeEnum.CONVERT_TO_BYTE.getCode().equals(fieldConversion.getType())){
                        ReflectionUtils.setFieldValue(datum,fieldConversion.getFieldName(),cellData.getImageValue());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
